package day05.concurrent02;

import java.util.concurrent.*;

/**
 * 7.ThreadPoolExecutor 线程池 - 构造参数详解
 */
public class Demo06 {
    public static void main(String[] args) {
        //Executors.newCachedThreadPool();
        //1.创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                5,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue(2),
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.out.println("线程池已满，拒绝执行"+r);
                    }
                });
        System.out.println(pool);
        //2.提交任务
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务1");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务2");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务3");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务4");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务5");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务6");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务7");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+":任务8");
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(pool);
        try {
            Thread.sleep(15 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(pool);
    }
}
